home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format 19
/
af019.adf
/
ILBM>RAW.s
< prev
next >
Wrap
Text File
|
1978-04-04
|
11KB
|
501 lines
*********************************************************
* *
* ILBM>RAW converter, by T.Young 1990. *
* *
* Usage:ILBM>RAW <Source> <Destination> [M] *
* *
* It is recommended that you look at the *
* include files, before reading this source. *
* *
*********************************************************
incdir Include:
include LibraryOffsets/Dos.i
include LibraryOffsets/Exec.i
include Macros/LibraryOpenCloseMACROS.i
include Macros/DosMACROS.i
*Macros
ReadMacro MACRO ;The DOS library must be open
move.l FileHandle,d1
move.l \1,d2 ;Address to read into
move.l \2,d3 ;Length to read
Call Read
ENDM
FreeMemory MACRO
Active Exec_Base ;move.l Exec_Base,a6
move.l \1Address,a1
beq \1NotAllocated ;Only free memory if allocated
move.l \1Size,d0
Call FreeMem
\1NotAllocated
ENDM
Error MACRO
\1Error
DisplayTextCLIH CLIHandler,\1Text,\1TextEnd ;Included macro
bra CloseDeAllocate ;Skip other errors
ENDM
*Zero pointers so that we know what has been allocated or opened
clr.l BodyAddress ;This ensures that if the program
clr.l NewBodyAddress ;can be residented since it
clr.l RawAddress ;does not rely on "dc.l 0"
clr.l FileHandle ;zeroing the location
clr.l Mask
*Save length and address of parameters
move.l a0,FileName ;Address of parameters
move.l d0,d5 ;length
*Open library
LibraryOpen DOS_Name,Dos_Base ;See LibraryOpenCloseMACROS.i
beq ErrorEnd ;Send Danger signal
;If we can't open DOS
*Get CLI handler in order to print error messages
GetCLIHandler CLIHandler
*Get filenames from CLI parameters
move.l FileName,a0 ;Retrieve address of parameters
bsr SkipSpaces ;See subroutines
beq NoParametersError ;Give syntax
cmp.b #"?",(a0)
beq NoParametersError ;Give syntax
move.l a0,FileName
bsr FindSpace ;See the subroutines
beq ParametersError
bsr SkipSpaces
beq ParametersError
move.l a0,OutFile
bsr FindSpace
beq EndOfParameters
bsr SkipSpaces
beq EndOfParameters
cmp.b #$a,(a0)
beq EndOfParameters
cmp.b #"M",(a0)
beq MaskNeeded
cmp.b #"m",(a0)
bne ParametersError
MaskNeeded
move.l #"MASK",Mask ;Set flag if mask is needed
cmp.b #" ",1(a0)
beq EndOfParameters
cmp.b #$a,1(a0)
bne ParametersError
EndOfParameters
*Open the file
Active Dos_Base
move.l FileName,d1
move.l #Mode_Oldfile,d2
Call Open
move.l d0,FileHandle
beq SourceError
*Check IFF header
ReadMacro #Width,#12 ;Use as temporary storage
cmp.l #"CAT ",Width ;Check for concatenation
bne NotConcat
DisplayTextCLIH CLIHandler,ConcatText,ConcatTextEnd
ReadMacro #Width,#12
NotConcat
cmp.l #"FORM",Width ;Check this is IFF
bne NotIffError ;Error - Not IFF
cmp.l #"ILBM",Planes ;Check ILBM
bne NotILBMError
*Read chunk name and act accordingly
bclr.l #0,d5 ;Flag for Bitmap Header found
ChunkLoop
jsr ReadLong ;Read chunk name
tst.l d0 ;No data was read
beq EOFError ;Therefore; end of file
cmp.l #"BMHD",LongBuffer
beq BMHD ;BMHD must come before BODY
cmp.l #"BODY",Longbuffer ;As specified in ILBM
beq BODY
jsr ReadLong ;Read chunk length
move.l FileHandle,d1
move.l LongBuffer,d2
move.l #0,d3 ;Offset_Current is 0
Call Seek ;Skip to next chunk
bra ChunkLoop
*Get compression, width, height and number of planes
BMHD
bset.l #$0,d5 ;Set flag for BMHD found
jsr ReadLong
ReadMacro #Width,#12 ;Read information
jsr ReadLong
jsr ReadLong
bra ChunkLoop
*Deal with Body data
BODY
;Check BMHD was found
btst.l #0,d5 ;Test flag
beq BMHDError
;Allocate Memory to read from file
jsr ReadLong ;Length of BODY data
Active Exec_Base
move.l LongBuffer,d0
clr.l d1
Call AllocMem
move.l d0,BodyAddress
beq MemoryError
Active Dos_Base
ReadMacro d0,Longbuffer ;Read into address d0 size Longbuffer
move.l LongBuffer,BodySize ;i.e. read BODY data
;CloseFile
Active Dos_Base
move.l FileHandle,d1
Call Close
clr.l FileHandle ;Ensure close-down routine knows
;Calculate size of decompressed data
clr.l d0
move.w Width,d0
move.w d0,d1
lsr.w #4,d0 ;Calculate width, in bytes
and.w #$f,d1
tst.w d1
beq EvenAlready
addq.w #1,d0 ;round up to account for pad bytes
clr.w d1
EvenAlready
lsl.w #1,d0
move.l d0,ByteWidth
mulu Height,d0 ;Therefore calculate size of one
move.l d0,PlaneSize ;bitplane, in bytes
move.b Planes,d1
mulu d1,d0
move.l d0,NewBodySize ;Therefore calculate size
move.l d0,d1 ;of data, when decompressed
cmp.l #"MASK",Mask
bne NoMaskPlane ;If a mask plane is required,
add.l PlaneSize,d1 ;add another plane to the size
NoMaskPlane
move.l d1,RawSize
;Uncompressed? - skip decompression
cmp.b #1,Compression ;Read from BMHD
blt Uncompressed
bgt CompressionError ;non standard compression
Active Exec_Base
clr.l d1
move.l NewBodySize,d0
Call AllocMem ;Allocate the memory for the
move.l d0,NewBodyAddress ;decompressed data
beq MemoryError
*Decompress
move.l NewBodyAddress,a1
move.l a1,a2
add.l NewBodySize,a2
move.l BodyAddress,a3
ByteLoop ;For an explanation of IFF
move.b (a3)+,d2 ;byte-run compression
bge VariedBytes ;please consult;
move.b (a3)+,d3 ;Amiga ROM Kernel Reference Manual
;Includes & Autodocs
ByteRunLoop
move.b d3,(a1)+
addq.b #1,d2
cmp.b #1,d2
bne ByteRunLoop
cmp.l a1,a2
bgt ByteLoop ;Decompression loop over ran
bra FinishedDecompression ;This is not good
VariedBytes
VariedBytesLoop
move.b (a3)+,(a1)+
subq.b #1,d2
cmp.b #-1,d2
bne VariedBytesLoop
cmp.l a1,a2
bgt ByteLoop ;Decompression loop over-ran
;This should not happen
FinishedDecompression
bne FaultyIffError
;DeAllocate memory containing Compressed data
move.l BodyAddress,a1
move.l BodySize,d0
Call FreeMem
move.l NewBodyAddress,BodyAddress ;Make decompression section
move.l NewBodySize,BodySize ;transparent
clr.l NewBodyAddress ;Tell system we have
;deallocated memory
UnCompressed
*Convert to Raw
;Allocate memory to hold raw bitplanes
Active Exec_Base
move.l RawSize,d0
clr.l d1
Call AllocMem
move.l d0,RawAddress
;Convert
move.l BodyAddress,a0 ;Source start address
move.l d0,a1 ;Destination start address
sub.l ByteWidth,d0
add.l NewBodySize,d0 ;Destination end address
move.l d0,a2 ;to signal when finished
move.b #1,d0 ;Bit Plane counter
ConvertLoop
clr.l d2 ;Again - consult Manual
WidthLoop ;concerning specifics
move.b (a0)+,(a1,d2) ;of ILBM specifications
addq.l #1,d2
cmp.l ByteWidth,d2
blt WidthLoop
bgt ConvertError ;Failsafe if BODY data is faulty
NoError
;Next Plane?
cmp.b Planes,d0
beq NoMorePlanes
addq.b #1,d0
add.l PlaneSize,a1
bra ConvertLoop
NoMorePlanes
;Finished?
cmp.l a1,a2
beq FinishedConvert
move.b #1,d0
add.l PlaneSize,a1
sub.l NewBodySize,a1
add.l ByteWidth,a1
bra ConvertLoop
FinishedConvert
;Add mask plane if wanted
cmp.l #"MASK",Mask
bne NoMask
clr.w d0
clr.w d3
move.b Planes,d3
subq.b #2,d3 ;Loop Planes-1 times
MaskByteLoop
move.l RawAddress,a0
move.b (a0,d0),d1
move.w d3,d2
MaskPlanesLoop ;Mask plane is result
add.l PlaneSize,a0 ;of all the other planes
or.b (a0,d0),d1 ;logically `OR'ed
dbra d2,MaskPlanesLoop
add.l PlaneSize,a0
move.b d1,(a0,d0)
addq.w #1,d0
cmp.w PlaneSize+2,d0
bne MaskByteLoop
NoMask
;Save RAW data
Active Dos_Base
move.l OutFile,d1
move.l #Mode_Newfile,d2 ;Attempt to create file
Call Open
move.l d0,FileHandle ;Not possible
beq DestinationError
move.l d0,d1
move.l RawAddress,d2
move.l RawSize,d3
Call Write ;Write converted data
*CloseDown
bra CloseDeAllocate ;Skip error messages
*Print text in case of an error.
Error NoParameters ;See macro at the top
Error Parameters ;of this source
Error Source
Error Destination ;Prints out text concerned
Error NotIFF ;and branches past the
Error NotILBM ;other errors
Error EOF
Error BMHD
Error Compression
Error FaultyIFF
Error Convert
Error Memory
CloseDeAllocate ;Close whatever is open
Active Dos_Base
move.l FileHandle,d1
beq FileNotOpen
Call Close
FileNotOpen
FreeMemory Raw ;See Macro
FreeMemory Body
FreeMemory NewBody
CloseDos
LibraryClose Dos_Base
rts
ErrorEnd
move.w #$ff,d0 ;If we cannot open DOS,
RedFlashLoop ;we cannot give error messages,
move.w #$f00,$dff180 ;so we give a danger signal instead
dbra d0,RedFlashLoop ;$dff180 is the hardware register
rts ;which holds the background colour
*Subroutines
SkipSpaces ;Increase a0 until it no longer points
cmp.b #" ",(a0)+ ;to a space
bne NoMoreSpaces
subq.l #1,d5 ;Decrease d5 each space
bne SkipSpaces ;Return if d5 becomes zero
rts ;Note - Zero flag is set
NoMoreSpaces
subq.l #1,a0 ;Note - Zero flag is cleared
rts
FindSpace ;Increase a0 until it points to a space
cmp.b #" ",(a0)+
beq SpaceFound
subq.l #1,d5 ;Decrease d5 each time
bne FindSpace ;Return if d5 becomes zero
clr.b -1(a0) ;Each parameter must be null terminated
rts ;For use as a filename
SpaceFound ;Note - Zero flag set
clr.b -1(a0)
subq.l #1,d5 ;Zero flag is set only if d5 becomes zero
rts ;Otherwise it is cleared
ReadLong
move.l FileHandle,d1 ;Read 4 bytes into Longbuffer
move.l #LongBuffer,d2
move.l #4,d3
Call Read
rts
*Data
Width dc.w 0 ;These must be in
Height dc.w 0 ;this order since
LongBuffer dc.l 0 ;they are read in
Planes dc.w 0 ;one go, 12 bytes
Compression dc.w 0 ;from width.
FileName dc.l 0
OutFile dc.l 0
BodyAddress dc.l 0
NewBodyAddress dc.l 0
RawAddress dc.l 0
NewBodySize dc.l 0
BodySize dc.l 0
RawSize dc.l 0
PlaneSize dc.l 0
ByteWidth dc.l 0
Mask dc.l 0
FileHandle dc.l 0
CLIHandler dc.l 0
Dos_Library_Data
*Text
ConcatText
dc.b "File is an IFF concatenation.",10,13
dc.b "Assuming first FORM is required.",10,13
ConcatTextEnd
even
*Error Messages
FaultyIFFText
dc.b "Error - Faulty IFF compression, decompression loops overrun.",10,13
FaultyIFFTextEnd
even
ConvertText
dc.b "Error - Faulty IFF interleave, conversion loop overrun.",10,13
ConvertTextEnd
NoParametersText
dc.b "ILBM to RAW converter, Copyright T.Young 1990.",10,13
dc.b "Usage: ILBM>RAW <Source> <Destination> [M]",10,13
NoParametersTextEnd
ParametersText
dc.b "Error - mistake in parameters.",10,13
dc.b "Usage: ILBM>RAW <Source> <Destination> [M]",10,13
ParametersTextEnd
even
SourceText
dc.b "Error - Cannot open source file.",10,13
SourceTextEnd
even
DestinationText
dc.b "Error - Cannot open destination file.",10,13
DestinationTextEnd
even
MemoryText
dc.b "Error - Out Of Memory.",10,13
MemoryTextEnd
even
NotIFFText
dc.b "Error - Source file not IFF.",10,13
NotIFFTextEnd
even
NotILBMText
dc.b "Error - Source file not ILBM.",10,13
NotILBMTextEnd
even
EOFText
dc.b "Error - No IFF BODY chunk.",10,13
EOFTextEnd
even
BMHDText
dc.b "Error - No IFF bitmap header before BODY.",10,13
BMHDTextEnd
even
CompressionText
dc.b "Error - Not IFF standard compression.",10,13
CompressionTextEnd
even